<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>03计算器练习</title>
</head>
<body>
<input type="text" id="s1" placeholder="请输入第一个数字"><br>
<input type="text" id="s2" placeholder="请输入第二个数字">
<br>
<button onclick="calc('+')">加</button>
<button onclick="calc('-')">减</button>
<button onclick="calc('*')">乘</button>
<button onclick="calc('/')">除</button>
<h1>运算结果:<span></span></h1>
<script>

    function calc(n){
        let input1=document.getElementById('s1');
        let input2=document.getElementById('s2');
        let x=parseFloat(input1.value);
        let y=parseFloat(input2.value);
        console.log(x);
        console.log(y);
        let spanE=document.querySelector('span');
        switch (n){
            case '+':spanE.innerHTML=x+y;break;
            case '-':spanE.innerHTML=x-y;break;
            case '*':spanE.innerHTML=x*y;break;
            case '/':
                if(y!=0){
                    spanE.innerHTML=x/y;
                }else {
                    alert("除数不能为0")
                }
        }
    }
</script>
</body>
</html>